﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CMS
{
    public class GroupNews : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "GroupNews";
            }
        }

        public override bool IsMainWebPart
        {
            get
            {
                return true;
            }
        }

        public override string Title
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Title", "Nhóm tin tức / menu");
            }
        }

        public override string Description
        {
            get
            {
                return ResourceBLL.GetText(WebPartId, "Description", "");
            }
        }


        public override WebPartInt[] SubWebParts
        {
            get
            {
                WebPartInt[] SubWebParts = new WebPartInt[]{
                    new AddGroupNews(),new UpdateGroupNews(),new AddGroupNewsItem(),new UpdateGroupNewsItem(),new GroupNewsHtml(),new UploadImageForGroupNews()
                };
                return SubWebParts;
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string belongSite)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                Environments.CheckWebPartAllow(WebPartId, true);

                string SelectSite =
                   "<select id=drpSelectSiteGroupNews onchange=javascript:FilterFunc();>";
                string[] Sites = Environments.GetSites();
                for (int iIndex = 0; iIndex < Sites.Length; iIndex++)
                {
                    if (Sites[iIndex].ToLower().Equals(belongSite))
                    {
                        SelectSite += "<option selected value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                    else
                    {
                        SelectSite += "<option value=\"" + Sites[iIndex] + "\">" + Sites[iIndex] + "</option>";
                    }
                }
                SelectSite += "</select>";
                string RetHtml =
                    "   <div>" + ResourceBLL.GetText(WebPartId, "SelectSite", "Site") + ":" + SelectSite + "&nbsp;<input type=button value=\"" + ResourceBLL.GetText(WebPartId, "Go", "Lọc dữ liệu") + "\" onclick=javascript:FilterFunc();>&nbsp;<input type=button onclick=\"javascript:ClientAddGroupNews();\" value=\"" + ResourceBLL.GetText(WebPartId, "TH_Add", "Thêm") + "\"></div>" +
                    "   <div id=divProcessing></div>";
                DataTable dtGroupNews = new GroupNewsBLL().Reading(belongSite);
                if (dtGroupNews.Rows.Count == 0)
                {
                    RetHtml += "<div class=\"cssNoData\">" + ResourceBLL.GetText(WebPartId, "NoData", "Chưa có dữ liệu nào trong chuyên mục này!") + "</div>";
                }
                else
                {
                    RetHtml +=

                    "   <table>" +
                    "       <tr>" +
                    "           <th>" + ResourceBLL.GetText(WebPartId, "TH_Subject", "Tiêu đề") + "</th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                    "           <th></th>" +
                        //"           <th></th>" +
                    "       </tr>";


                    for (int iIndex = 0; iIndex < dtGroupNews.Rows.Count; iIndex++)
                    {
                        string GroupNewsId = (string)dtGroupNews.Rows[iIndex]["GroupNewsId"];
                        belongSite = (string)dtGroupNews.Rows[iIndex]["belongSite"];
                        string Subject = (string)dtGroupNews.Rows[iIndex]["Subject"];
                        string DeleteUrl = "javascript:ClientDeleteGroupNews('" + GroupNewsId + "')";
                        string PermissionUrl = "javascript:ShowPermissionFuncForm('" + GroupNewsId + "')";
                        RetHtml +=
                        "       <tr onMouseOver=\"this.className='highlight'\" onMouseOut=\"this.className='normal'\">" +
                        "           <td>" + Subject + "</td>" +
                        "           <td><a href=\"javascript:ShowClientUpdateUrlForm('" + GroupNewsId + "');\">" + ResourceBLL.GetText(WebPartId, "TH_Update", "Cập nhật") + "</a></td>" +
                        "           <td><a href=\"" + DeleteUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Delete", "Xóa") + "</a></td>" +
                        "           <td><a href=\"" + PermissionUrl + "\">" + ResourceBLL.GetText(WebPartId, "TH_Permission", "Phân quyền") + "</a></td>" +
                        "           <td><a href=\"javascript:ShowClientGroupNewsItemUrlForm('" + GroupNewsId + "');\">" + ResourceBLL.GetText(WebPartId, "ShowGroupNewsItemCaption", "Xem chi tiết") + "</a></td>" +
                            //"           <td><a href=\"javascript:ClientShowNewsItemForms('" + GroupNewsId + "');\">" + ResourceBLL.GetText(WebPartId,"ShowNewsItemText","Tin tức") + "</a></td>" +
                            //"           <td><a href=\"javascript:GenerateNewsStaticPage('" + GroupNewsId + "');\">" + ResourceBLL.GetText(WebPartId, "GenerateStaticNewsPage", "Tạo trang tĩnh") + "</a></td>" +
                        "           <td></td>" +
                        "       </tr>";
                    }
                    RetHtml +=
                        "   </table>";
                }
                return RetHtml;
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

        public override string RenderWebPart(LoadParamCls loadParamCls)
        {
            Environment.Environments.CheckSessionTimeOut(true);
            AjaxPro.Utility.RegisterTypeForAjax(typeof(GroupNews));
            string ActiveSite = Portal.Environment.Environments.GetActiveSite();
            string RetHtml =
                WebPartUtilities.AttachJavascript("Portal.CMS", "GroupNews.js") +
                "<div id=divGroupNewsMain class=cssDivGroupNews>" + GetHtml(ActiveSite) + "</div>";
            return RetHtml;
        }

        [AjaxPro.AjaxMethod]
        public string GetAddGroupNewUrl(string belongSite)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = FindPermissionType("GroupNews");
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "AddGroupNews", WebPartId, null, true);

                return Screen.BuildDialogUrl(new ParamCls[]{
                    new ParamCls("WebPartId",new AddGroupNews().WebPartId),
                    new ParamCls("belongSite",belongSite)
            });
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetConfirmDeleteMsg()
        {
            return ResourceBLL.GetText(WebPartId, "ConfirmDeleteMsg", "Bạn đã chắc chắn xóa chưa?");
        }


        [AjaxPro.AjaxMethod]
        public string ServerSideCheckAllowDelete(string GroupNewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new GroupNews().PermissionTypes, "GroupNewsItems");
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteGroupNewsItem", GroupNewsId, GroupNewsCls.Owner, true);

                return "";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideDeleteGroupNews(string GroupNewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new GroupNews().PermissionTypes, "GroupNewsItems");
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "DeleteGroupNewsItem", GroupNewsId, GroupNewsCls.Owner, true);

                SqlTransaction sqlTransaction = Environment.Environments.CreateTransaction();
                try
                {
                    new GroupNewsBLL().DeleteGroupNews(sqlTransaction, GroupNewsId);
                    sqlTransaction.Commit();
                }
                catch (Exception ex)
                {
                    sqlTransaction.Rollback();
                    throw (ex);
                }
                return ResourceBLL.GetText(WebPartId, "DeleteOkMessage", "Xóa thành công!");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowUpdateForm(string GroupNewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new GroupNews().PermissionTypes, "GroupNewsItems");
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateGroupNewsItem", GroupNewsId, GroupNewsCls.Owner, true);

                string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new UpdateGroupNews().WebPartId),
                        new ParamCls("GroupNewsId",GroupNewsId)
                    });
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }


        [AjaxPro.AjaxMethod]
        public string GetShowNewsItemForm(string GroupNewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new GroupNews().PermissionTypes, "GroupNewsItems");
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "ManageNews", GroupNewsId, GroupNewsCls.Owner, true);

                string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId","News"),
                        new ParamCls("GroupNewsId",GroupNewsId)
                    });
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }



        [AjaxPro.AjaxMethod]
        public string GetShowGroupNewsItemsForm(string GroupNewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new GroupNews().PermissionTypes, "GroupNewsItems");
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "UpdateGroupNewsItem", GroupNewsId, GroupNewsCls.Owner, true);

                string UpdateUrl = Screen.BuildDialogUrl(new ParamCls[]{
                        new ParamCls("WebPartId",new GroupNewsItems().WebPartId),
                        new ParamCls("GroupNewsId",GroupNewsId)
                    });
                return UpdateUrl;
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }



        public override FunctionCls[] FunctionList
        {
            get
            {
                return new FunctionCls[]
                {
                    new FunctionCls("PermissionGroupNews",ResourceBLL.GetText(WebPartId,"PermissionGroupNews","Phân quyền nhóm tin tức"),WebPartId),
                    new FunctionCls("AddGroupNews",ResourceBLL.GetText(WebPartId,"AddGroupNews","Thêm mới nhóm tin tức"),WebPartId),

                    new FunctionCls("ViewGroupNews",ResourceBLL.GetText(WebPartId,"ViewGroupNews","Xem nhóm tin tức"),WebPartId),
                    new FunctionCls("UpdateGroupNewsItem",ResourceBLL.GetText(WebPartId,"UpdateGroupNewsItem","Cập nhật chi tiết nhóm tin tức"),WebPartId),
                    new FunctionCls("DeleteGroupNewsItem",ResourceBLL.GetText(WebPartId,"DeleteGroupNewsItem","Xóa chi tiết nhóm tin tức"),WebPartId),
                    new FunctionCls("PermissionGroupNewsItem",ResourceBLL.GetText(WebPartId,"PermissionGroupNewsItem","Phân quyền cho từng bản ghi nhóm tin tức"),WebPartId),
                    new FunctionCls("ManageNews",ResourceBLL.GetText(WebPartId,"ManageNews","Quản lý tin tức trong nhóm"),WebPartId),
                    new FunctionCls("MakeStaticPage",ResourceBLL.GetText(WebPartId,"MakeStaticPage","Tạo trang tĩnh"),WebPartId),
                    new FunctionCls("AddNewsForGroup",ResourceBLL.GetText(WebPartId,"AddNewsForGroup","Tạo mới tin tức (theo nhóm)"),WebPartId),
                    new FunctionCls("AddSubGroupNewsItem",ResourceBLL.GetText(WebPartId,"AddSubGroupNewsItem","Thêm mới nhóm con"),WebPartId),
                    new FunctionCls("PublishingNews",ResourceBLL.GetText(WebPartId,"PublishingNews","Phát hành tin tức (theo nhóm)"),WebPartId),
                    new FunctionCls("UpdateNewsItem",ResourceBLL.GetText(WebPartId,"UpdateNewsItem","Sửa chữa tin tức thuộc nhóm"),WebPartId),
                    new FunctionCls("DeleteNewsItem",ResourceBLL.GetText(WebPartId,"DeleteNewsItem","Xoá tin tức thuộc nhóm"),WebPartId),

                  
                };
            }
        }

        public override PermissionTypeCls[] PermissionTypes
        {
            get
            {
                return new PermissionTypeCls[]{
                    new PermissionTypeCls("GroupNewsItems","Nhóm tin tức (chi tiết)",new string[]{"PermissionGroupNewsItem","UpdateGroupNewsItem","DeleteGroupNewsItem","ManageNews","MakeStaticPage","AddNewsForGroup","AddSubGroupNewsItem","PublishingNews","UpdateNewsItem","DeleteNewsItem"}),
                    new PermissionTypeCls("GroupNews","Nhóm tin tức",new string[]{"AddGroupNews","PermissionGroupNews"})
                };
            }
        }

        [AjaxPro.AjaxMethod]
        public string GetShowFormSetPermission(string GroupNewsId)
        {
            try
            {
                Environment.Environments.CheckSessionTimeOut(true);
                PermissionTypeCls PermissionTypeCls = Environments.FindPermissionType(new GroupNews().PermissionTypes, "GroupNewsItems");
                GroupNewsCls GroupNewsCls = new GroupNewsBLL().CreateModel(GroupNewsId);
                Environment.Permission.CheckPermission(Environment.Environments.CurrentLoginUser.LoginName, PermissionTypeCls.PermissionTypeCode, "PermissionGroupNewsItem", GroupNewsId, GroupNewsCls.Owner, true);
                return Environment.Permission.GetPermissionUrl(WebPartId, GroupNewsId, "GroupNewsItems");
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

        public override PermissionTypeCls GetServicePermissionType()
        {
            return PermissionTypes[1];
        }

        [AjaxPro.AjaxMethod]
        public string GetProcessingText()
        {
            try
            {
                Environments.CheckSessionTimeOut(true);
                string Url = Environment.Environments.GetActiveThemeUrl() + "/Global/Loading.gif";
                return "<img src=\"" + Url + "\"><a class=cssProcessing>" + ResourceBLL.GetText(WebPartId, "ProcessingText", "Đang đọc dữ liệu...") + "</a>";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }
    }
}